﻿using System.Drawing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OrmTestDomain;
using Console = Colorful.Console;

namespace FreeSqlTest.BackgroundServices;

public class DynamicLinqTestService : BackgroundService
{
    private readonly IFreeSql _freeSql;
    private readonly ILogger<DynamicLinqTestService> _logger;

    public DynamicLinqTestService(ILogger<DynamicLinqTestService> logger, IFreeSql freeSql)
    {
        _logger = logger;
        _freeSql = freeSql;
    }

    private void PrepareUser()
    {
        var baseTable = _freeSql.CodeFirst.GetTableByEntity(typeof(UserEntity));
        _freeSql.CodeFirst.GetTableByEntity(typeof(UserSplitEntity)).AsTableImpl
            .SetTableName(0, baseTable.DbName);


        var roles = new List<RoleEntity>();
        var random = new Random();
        var rolesCount = random.Next(1, 10);
        for (var i = 0; i < rolesCount; i++)
        {
            roles.Add(new RoleEntity());
        }

        var userCount = random.Next(1, 100);
        var userExtends = new List<UserExtendEntity>();
        var users = new List<UserSplitEntity>();
        for (var i = 0; i < userCount; i++)
        {
            var roleIndex = random.Next(1, rolesCount) - 1;
            var randomCreateTime = DateTime.Now.AddDays(random.Next(-12, 12));
            var userEntity = new UserSplitEntity { CreateTime = randomCreateTime };

            var age = random.Next(1, 100);
            var userExtendEntity = new UserExtendEntity
            {
                UserId = userEntity.Id,
                UserRoleId = roles[roleIndex].Id,
                Age = age,
                BirthDate = DateTime.Today.AddYears(-age)
            };
            userExtends.Add(userExtendEntity);
            users.Add(userEntity);
        }

        Console.WriteLine(_freeSql.InsertOrUpdate<UserExtendEntity>().SetSource(userExtends).ExecuteAffrows(),
                          Color.Aqua);
        Console.WriteLine(_freeSql.InsertOrUpdate<UserSplitEntity>().SetSource(users).ExecuteAffrows(), Color.Brown);
        Console.WriteLine(_freeSql.InsertOrUpdate<RoleEntity>().SetSource(roles).ExecuteAffrows(), Color.Azure);
    }


    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var NatashaQueryBuilder = new NatashaQueryBuilder();
        var dynamicList = NatashaQueryBuilder.BuilerBuiderSelect(_freeSql);

        var select =
            _freeSql.Select<UserSplitEntity, UserExtendEntity, RoleEntity>()
                .LeftJoin((a, b, c) => b.UserId == a.Id)
                .LeftJoin((a, b, c) => b.UserRoleId == c.Id)
                .Where((a, b, c) => c.Id > 0);


        var list = select
            .ToList((a, b, c) => new UseDto
            {
                BirthDate = b.BirthDate,
                Age = b.Age,
                Id = a.Id,
                Name = a.Name,
                RoleName = c.RoleName
            });

        Console.WriteLine($"list.Count:{list.Count},dynamicList.Count:{dynamicList.Count}");


        return Task.CompletedTask;
    }
}

public class UseDto
{
    public long Id { get; set; }

    public string Name { get; set; }

    public DateTime BirthDate { get; set; }

    public int Age { get; set; }
    public string RoleName { get; set; }
}
